package com.dtruth.dataflow.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class FileUtil {

	public static List<String> read(File file) {
		BufferedReader br = null;
		StringBuffer sb = new StringBuffer();
		List<String> list = new ArrayList<String>();
		try {
			br = new BufferedReader(new InputStreamReader(new FileInputStream(
					file), "utf8"));
			String line = null;
			int i = 0;
			while ((line = br.readLine()) != null) {
				if (i > 3) {
					String text = sb.toString().trim();
					if (text.length() > 5) {
						list.add(text);
					}
					sb = null;
					sb = new StringBuffer();
					i = 0;
				}
				if (line.trim().equals("") || line.trim().length() < 1) {
					i++;
					continue;
				}
				sb.append(line);
			}
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}

	public static void write(String path, String text, String category) {
		try {
			FileWriter fw = new FileWriter(path + "/result/" + category
					+ ".txt");
			fw.write(text);
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
